package com.sc.test.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.sc.test.R;
import com.sc.test.utils.Util;

/**
 * Created by Shen Chao
 * Created by 2019-03-07
 */
public class ShadeView extends View {
    private Paint mDstPaint, mSrcPaint;
    private Canvas mSrcCanvas, mDstCanvas;
    private Bitmap mSrcBitmap, mDstBitmap;
    int radius = Util.dpToPx(20);


    public ShadeView(Context context) {
        this(context, null);
    }

    public ShadeView(Context context, AttributeSet attrs) {
        this(context, null, 0);
    }

    public ShadeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mDstPaint = new Paint();
        mSrcPaint = new Paint();
        mDstPaint.setColor(Color.BLUE);
        mSrcPaint.setColor(Color.RED);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = getHeight();
        int width = getWidth();
        int ocrHeight = getResources().getDimensionPixelOffset(R.dimen.ocr_height);
        int ocrWidth = getResources().getDimensionPixelOffset(R.dimen.ocr_width);


        mSrcBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        mSrcCanvas = new Canvas(mSrcBitmap);
        Bitmap mDstBitmap1 = Bitmap.createBitmap(ocrWidth, ocrHeight, Bitmap.Config.RGB_565);
//        Bitmap bitmap = Bitmap.createBitmap(ocrWidth, ocrHeight, Bitmap.Config.RGB_565);
//        mDstBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ocr_shape);
//        Drawable drawable =  getResources().getDrawable(R.drawable.ocr_shape);
//        BitmapDrawable bd = (BitmapDrawable) drawable;
//        mDstBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ocr_shape));
//        mDstBitmap = getRoundedCornerBitmap(bitmap, radus);
        RoundImageDrawable roundImageDrawable = new RoundImageDrawable(mDstBitmap1, radius);
        mDstBitmap = drawableToBitmap(roundImageDrawable);
        mDstCanvas = new Canvas(mDstBitmap);

        int mDstRectFLeft = width / 2 - ocrWidth / 2;
        int mDstRectFRight = width / 2 + ocrWidth / 2;
        int mDstRectFTop = height / 2 - ocrHeight / 2;
        int mDstRectFBottom = height / 2 + ocrHeight / 2;

        int mSrcRectFLeft = 0;
        int mSrcRectFRight = width;
        int mSrcRectFTop = 0;
        int mSrcRectFBottom = height;

        mSrcCanvas.drawRect(mSrcRectFLeft, mSrcRectFTop, mSrcRectFRight, mSrcRectFBottom, mSrcPaint);
        canvas.drawBitmap(mSrcBitmap, 0, 0, mSrcPaint);

        mDstPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));

        mDstCanvas.drawRoundRect(0, 0, ocrWidth, ocrHeight, radius, radius, mDstPaint);
        canvas.drawBitmap(mDstBitmap, mDstRectFLeft, mDstRectFTop, mDstPaint);
    }

    public Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的长宽
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        // 取 drawable 的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }


}
